home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / vidhrdw / digdug.c < prev    next >
C/C++ Source or Header  |  2000-04-04  |  9KB  |  329 lines

  1. /***************************************************************************
  2.  
  3.   vidhrdw.c
  4.  
  5.   Functions to emulate the video hardware of the machine.
  6.  
  7. ***************************************************************************/
  8.  
  9. #include "driver.h"
  10. #include "vidhrdw/generic.h"
  11.  
  12.  
  13. unsigned char *digdug_vlatches;
  14. static int playfield, alphacolor, playenable, playcolor;
  15.  
  16. static int pflastindex = -1, pflastcolor = -1;
  17. static int flipscreen;
  18.  
  19.  
  20. /***************************************************************************
  21.  
  22.   Convert the color PROMs into a more useable format.
  23.  
  24.   digdug has one 32x8 palette PROM and two 256x4 color lookup table PROMs
  25.   (one for characters, one for sprites). Only the first 128 bytes of the
  26.   lookup tables seem to be used.
  27.   The palette PROM is connected to the RGB output this way:
  28.  
  29.   bit 7 -- 220 ohm resistor  -- BLUE
  30.         -- 470 ohm resistor  -- BLUE
  31.         -- 220 ohm resistor  -- GREEN
  32.         -- 470 ohm resistor  -- GREEN
  33.         -- 1  kohm resistor  -- GREEN
  34.         -- 220 ohm resistor  -- RED
  35.         -- 470 ohm resistor  -- RED
  36.   bit 0 -- 1  kohm resistor  -- RED
  37.  
  38. ***************************************************************************/
  39. void digdug_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom)
  40. {
  41.     int i;
  42.  
  43.     for (i = 0;i < 32;i++)
  44.     {
  45.         int bit0,bit1,bit2;
  46.  
  47.         bit0 = (color_prom[31-i] >> 0) & 0x01;
  48.         bit1 = (color_prom[31-i] >> 1) & 0x01;
  49.         bit2 = (color_prom[31-i] >> 2) & 0x01;
  50.         palette[3*i] = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
  51.         bit0 = (color_prom[31-i] >> 3) & 0x01;
  52.         bit1 = (color_prom[31-i] >> 4) & 0x01;
  53.         bit2 = (color_prom[31-i] >> 5) & 0x01;
  54.         palette[3*i + 1] = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
  55.         bit0 = 0;
  56.         bit1 = (color_prom[31-i] >> 6) & 0x01;
  57.         bit2 = (color_prom[31-i] >> 7) & 0x01;
  58.         palette[3*i + 2] = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
  59.     }
  60.  
  61.     /* characters */
  62.     for (i = 0; i < 8; i++)
  63.     {
  64.         colortable[i*2 + 0] = 0;
  65.         colortable[i*2 + 1] = 31 - i*2;
  66.     }
  67.     /* sprites */
  68.     for (i = 0*4;i < 64*4;i++)
  69.         colortable[8*2 + i] = 31 - ((color_prom[i + 32] & 0x0f) + 0x10);
  70.     /* playfield */
  71.     for (i = 64*4;i < 128*4;i++)
  72.         colortable[8*2 + i] = 31 - (color_prom[i + 32] & 0x0f);
  73. }
  74.  
  75.  
  76.  
  77. /***************************************************************************
  78.  
  79.   Start the video hardware emulation.
  80.  
  81. ***************************************************************************/
  82. int digdug_vh_start(void)
  83. {
  84.     if (generic_vh_start() != 0)
  85.         return 1;
  86.  
  87.     pflastindex = -1;
  88.     pflastcolor = -1;
  89.  
  90.     return 0;
  91. }
  92.  
  93.  
  94. /***************************************************************************
  95.  
  96.   Stop the video hardware emulation.
  97.  
  98. ***************************************************************************/
  99. void digdug_vh_stop(void)
  100. {
  101.     generic_vh_stop();
  102. }
  103.  
  104.  
  105. WRITE_HANDLER( digdug_vh_latch_w )
  106. {
  107.     switch (offset)
  108.     {
  109.         case 0:
  110.             playfield = (playfield & ~1) | (data & 1);
  111.             break;
  112.  
  113.         case 1:
  114.             playfield = (playfield & ~2) | ((data << 1) & 2);
  115.             break;
  116.  
  117.         case 2:
  118.             alphacolor = data & 1;
  119.             break;
  120.  
  121.         case 3:
  122.             playenable = data & 1;
  123.             break;
  124.  
  125.         case 4:
  126.             playcolor = (playcolor & ~1) | (data & 1);
  127.             break;
  128.  
  129.         case 5:
  130.             playcolor = (playcolor & ~2) | ((data << 1) & 2);
  131.             break;
  132.     }
  133. }
  134.  
  135.  
  136. void digdug_draw_sprite(struct osd_bitmap *dest,unsigned int code,unsigned int color,
  137.     int flipx,int flipy,int sx,int sy)
  138. {
  139.     drawgfx(dest,Machine->gfx[1],code,color,flipx,flipy,sx,sy,&Machine->drv->visible_area,
  140.         TRANSPARENCY_PEN,0);
  141. }
  142.  
  143.  
  144.  
  145. WRITE_HANDLER( digdug_flipscreen_w )
  146. {
  147.     if (flipscreen != (data & 1))
  148.     {
  149.         flipscreen = data & 1;
  150.         memset(dirtybuffer,1,videoram_size);
  151.     }
  152. }
  153.  
  154. /***************************************************************************
  155.  
  156.   Draw the game screen in the given osd_bitmap.
  157.   Do NOT call osd_update_display() from this function, it will be called by
  158.   the main emulation engine.
  159.  
  160. ***************************************************************************/
  161. void digdug_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
  162. {
  163.     int offs,pfindex,pfcolor;
  164.     unsigned char *pf;
  165.  
  166.     /* determine the playfield */
  167.     if (playenable != 0)
  168.     {
  169.         pfindex = pfcolor = -1;
  170.         pf = NULL;
  171.     }
  172.     else
  173.     {
  174.         pfindex = playfield;
  175.         pfcolor = playcolor;
  176.         pf = memory_region(REGION_GFX4) + (pfindex << 10);
  177.     }
  178.  
  179.     /* force a full update if the playfield has changed */
  180.     if (pfindex != pflastindex || pfcolor != pflastcolor)
  181.     {
  182.         memset(dirtybuffer,1,videoram_size);
  183.     }
  184.     pflastindex = pfindex;
  185.     pflastcolor = pfcolor;
  186.  
  187.     pfcolor <<= 4;
  188.  
  189.     /* for every character in the Video RAM, check if it has been modified */
  190.     /* since last time and update it accordingly. */
  191.     for (offs = videoram_size - 1;offs >= 0;offs--)
  192.     {
  193.         if (dirtybuffer[offs])
  194.         {
  195.             unsigned char pfval, vrval;
  196.             int sx,sy,mx,my;
  197.  
  198.             dirtybuffer[offs] = 0;
  199.  
  200.             /* Even if digdug's screen is 28x36, the memory layout is 32x32. We therefore */
  201.             /* have to convert the memory coordinates into screen coordinates. */
  202.             /* Note that 32*32 = 1024, while 28*36 = 1008: therefore 16 bytes of Video RAM */
  203.             /* don't map to a screen position. We don't check that here, however: range */
  204.             /* checking is performed by drawgfx(). */
  205.  
  206.             mx = offs % 32;
  207.             my = offs / 32;
  208.  
  209.             if (my <= 1)
  210.             {
  211.                 sx = my + 34;
  212.                 sy = mx - 2;
  213.             }
  214.             else if (my >= 30)
  215.             {
  216.                 sx = my - 30;
  217.                 sy = mx - 2;
  218.             }
  219.             else
  220.             {
  221.                 sx = mx + 2;
  222.                 sy = my - 2;
  223.             }
  224.  
  225.             if (flipscreen)
  226.             {
  227.                 sx = 35 - sx;
  228.                 sy = 27 - sy;
  229.             }
  230.  
  231.             vrval = videoram[offs];
  232.             if (pf)
  233.             {
  234.                 /* first draw the playfield */
  235.                 pfval = pf[offs];
  236.                 drawgfx(tmpbitmap,Machine->gfx[2],
  237.                         pfval,
  238.                         (pfval >> 4) + pfcolor,
  239.                         flipscreen,flipscreen,
  240.                         8*sx,8*sy,
  241.                         &Machine->drv->visible_area,TRANSPARENCY_NONE,0);
  242.  
  243.                 /* overlay with the character */
  244.                 if ((vrval & 0x7f) != 0x7f)
  245.                     drawgfx(tmpbitmap,Machine->gfx[0],
  246.                             vrval,
  247.                             (vrval >> 5) | ((vrval >> 4) & 1),
  248.                             flipscreen,flipscreen,
  249.                             8*sx,8*sy,
  250.                             &Machine->drv->visible_area,TRANSPARENCY_PEN,0);
  251.             }
  252.             else
  253.             {
  254.                 /* just draw the character */
  255.                 drawgfx(tmpbitmap,Machine->gfx[0],
  256.                         vrval,
  257.                         (vrval >> 5) | ((vrval >> 4) & 1),
  258.                         flipscreen,flipscreen,
  259.                         8*sx,8*sy,
  260.                         &Machine->drv->visible_area,TRANSPARENCY_NONE,0);
  261.             }
  262.         }
  263.     }
  264.  
  265.     /* copy the temporary bitmap to the screen */
  266.     copybitmap(bitmap,tmpbitmap,0,0,0,0,&Machine->drv->visible_area,TRANSPARENCY_NONE,0);
  267.  
  268.     /* Draw the sprites. */
  269.     for (offs = 0;offs < spriteram_size;offs += 2)
  270.     {
  271.         /* is it on? */
  272.         if ((spriteram_3[offs+1] & 2) == 0)
  273.         {
  274.             int sprite = spriteram[offs];
  275.             int color = spriteram[offs+1];
  276.             int x = spriteram_2[offs+1]-40;
  277.             int y = 28*8-spriteram_2[offs];
  278.             int flipx = spriteram_3[offs] & 1;
  279.             int flipy = spriteram_3[offs] & 2;
  280.  
  281.             if (flipscreen)
  282.             {
  283.                 flipx = !flipx;
  284.                 flipy = !flipy;
  285.             }
  286.  
  287.             if (x < 8) x += 256;
  288.  
  289.             /* normal size? */
  290.             if (sprite < 0x80)
  291.                 digdug_draw_sprite(bitmap,sprite,color,flipx,flipy,x,y);
  292.  
  293.             /* double size? */
  294.             else
  295.             {
  296.                 sprite = (sprite & 0xc0) | ((sprite & ~0xc0) << 2);
  297.                 if (!flipx && !flipy)
  298.                 {
  299.                     digdug_draw_sprite(bitmap,2+sprite,color,flipx,flipy,x,y);
  300.                     digdug_draw_sprite(bitmap,3+sprite,color,flipx,flipy,x+16,y);
  301.                     digdug_draw_sprite(bitmap,sprite,color,flipx,flipy,x,y-16);
  302.                     digdug_draw_sprite(bitmap,1+sprite,color,flipx,flipy,x+16,y-16);
  303.                 }
  304.                 else if (flipx && flipy)
  305.                 {
  306.                     digdug_draw_sprite(bitmap,1+sprite,color,flipx,flipy,x,y);
  307.                     digdug_draw_sprite(bitmap,sprite,color,flipx,flipy,x+16,y);
  308.                     digdug_draw_sprite(bitmap,3+sprite,color,flipx,flipy,x,y-16);
  309.                     digdug_draw_sprite(bitmap,2+sprite,color,flipx,flipy,x+16,y-16);
  310.                 }
  311.                 else if (flipy)
  312.                 {
  313.                     digdug_draw_sprite(bitmap,sprite,color,flipx,flipy,x,y);
  314.                     digdug_draw_sprite(bitmap,1+sprite,color,flipx,flipy,x+16,y);
  315.                     digdug_draw_sprite(bitmap,2+sprite,color,flipx,flipy,x,y-16);
  316.                     digdug_draw_sprite(bitmap,3+sprite,color,flipx,flipy,x+16,y-16);
  317.                 }
  318.                 else /* flipx */
  319.                 {
  320.                     digdug_draw_sprite(bitmap,3+sprite,color,flipx,flipy,x,y);
  321.                     digdug_draw_sprite(bitmap,2+sprite,color,flipx,flipy,x+16,y);
  322.                     digdug_draw_sprite(bitmap,1+sprite,color,flipx,flipy,x,y-16);
  323.                     digdug_draw_sprite(bitmap,sprite,color,flipx,flipy,x+16,y-16);
  324.                 }
  325.             }
  326.         }
  327.     }
  328. }
  329.